﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LeadVinaglue.Models;
using LeadVinaglue.Models.ViewPages;
using System.Web.Mvc;

namespace LeadVinaglue.Repository
{
    public class ContactManagementRepository : BaseManagementRepository
    {
        LeadVinaglueEntities entities = new LeadVinaglueEntities();

        public ContactManagementModel GetModel(string pageType, string roleName, int id)
        {
            ContactManagementModel model = new ContactManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
            model.ListContact = GetAll();

            if (id > 0)
            {
                model.ContactInfo = GetSingle(id);
            }

            return model;
        }

        public ContactManagementModel GetModel(string pageType, string roleName)
        {
            ContactManagementModel model = new ContactManagementModel();
            model.ListContact = GetAll();

            return model;
        }

        public List<ContactModel> GetAll()
        {
            List<ContactModel> list = new List<ContactModel>();

            var items = (from c in entities.Contacts
                         orderby c.DateCreated descending
                         select c).ToList();

            foreach (var item in items)
            {
                ContactModel model = new ContactModel();

                model.ID = item.ID;
                model.Name = item.Name;
                model.Phone = item.Phone;
                model.Email = item.Email;
                model.Active = item.Active;
                model.DateCreated = Convert.ToDateTime(item.DateCreated).ToString("dd-MM-yyyy hh:mm tt");
                model.Status = (item.Active) ? "Followed" : "New";
                list.Add(model);
            }

            return list;
        }

        public Contact GetSingle(int id)
        {
            return (from _object in entities.Contacts where _object.ID == id select _object).FirstOrDefault();
        }

        public Contact GetSingle(string name)
        {
            return (from _object in entities.Contacts where _object.Name == name select _object).FirstOrDefault();
        }

        public void Create([Bind(Exclude = "ID, DateCreated")] Contact _object)
        {
            _object.DateCreated = System.DateTime.Now;
            entities.AddToContacts(_object);
            entities.SaveChanges();
        }

        public void Delete(int id)
        {
            var _object = (from _list in entities.Contacts where _list.ID == id select _list).First();
            entities.DeleteObject(_object);
            entities.SaveChanges();
        }

        public void Save([Bind(Exclude = "ID")] Contact _object)
        {
            _object.EntityKey = (from Contact ac in entities.Contacts where ac.ID == _object.ID select ac).FirstOrDefault().EntityKey;            
            entities.ApplyPropertyChanges(_object.EntityKey.EntitySetName, _object);
            entities.SaveChanges();
        }
    }
}